package com.gears_software.sxdc.dccx.ui.activity;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;


import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.adapter.RechargeAmountAdapter;
import com.gears_software.sxdc.dccx.data.finder.IBankFinder;
import com.gears_software.sxdc.dccx.data.model.RechargeAmount;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.data.model.UserBalance;
import com.gears_software.sxdc.dccx.ui.util.MaterialDialogProgress;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.rest.spring.annotations.RestService;

import java.util.ArrayList;
import java.util.List;

import qiu.niorgai.StatusBarCompat;

/**
 * Created by chezhenzhen on 2017/6/14.
 */


@EActivity(R.layout.activity_chongzhi)
public class ChongZhiActivity extends AppCompatActivity {

    @App
    DCCXApplication app;
    @ViewById
    View actionbar_back_title;
    @ViewById
    TextView actionbar_title;
    @ViewById
    TextView back_icon;
    @ViewById
    TextView right_icon;
    @ViewById
    GridView gv_pay;
    @RestService
    IBankFinder bankFinder;
    @ViewById
    TextView tv_money;
    @ViewById
    Button btn_chongzhi;
    List<RechargeAmount> list = new ArrayList<>();
    RechargeAmountAdapter adapter;
    RechargeAmount rechargeAmount;
    MaterialDialogProgress progress;

    @AfterViews
    void created() {
        initIconFont();
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorActionbar));
        progress = new MaterialDialogProgress(this);
        progress.show("正在加载...");
        getUserBalance();
        RechargeAmount();

    }

    void initIconFont() {
        actionbar_title.setText("充值");
        back_icon.setVisibility(View.VISIBLE);
        right_icon.setVisibility(View.GONE);
        back_icon.setText(R.string.back_icon);
        back_icon.setTypeface(app.iconfont);
    }

    @Click
    void back_icon() {
        this.finish();
    }

    @Background
    public void getUserBalance() {
        ResultModel<UserBalance> res = bankFinder.getUserBalance();
        showResout(res);
    }

    @UiThread
    void showResout(ResultModel<UserBalance> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                if (res.getData() != null) {
                    UserBalance balance = res.getData();
                    tv_money.setText(balance.getUseramount() + "");
                }
            }
        }
        progress.dismiss();
    }

    @Background
    public void RechargeAmount() {
        ResultModel<List<RechargeAmount>> res = bankFinder.rechargeAmount();
        showRechargeResout(res);
    }

    @UiThread
    void showRechargeResout(ResultModel<List<RechargeAmount>> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                list = res.getData();
                rechargeAmount = list.get(0);
                for (int i = 0; i < list.size(); i++) {
                    if (i == 0) {
                        list.get(i).setSelect(true);
                    } else {
                        list.get(i).setSelect(false);
                    }
                }
                if (list != null && list.size() > 0) {
                    adapter = new RechargeAmountAdapter(this, list);
                    gv_pay.setAdapter(adapter);
                }
            }
        }
        progress.dismiss();
    }

    @ItemClick
    void gv_pay(int position) {
        rechargeAmount = list.get(position);
        for (int i = 0; i < list.size(); i++) {
            if (position == i) {
                list.get(i).setSelect(true);
            } else {
                list.get(i).setSelect(false);
            }
        }
        adapter.notifyDataSetChanged();
    }

    @Click
    void btn_chongzhi() {
        Intent intent = new Intent();
        intent.putExtra("id", rechargeAmount.getId());
        intent.putExtra("rechargeamount", rechargeAmount.getRechargeamount());
        intent.putExtra("giveamount", rechargeAmount.getGiveamount());
        intent.setClass(this, RechargeActivity_.class);
        startActivityForResult(intent, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            progress.show("正在加载...");
            getUserBalance();
        }
    }
}


